﻿using System;
using Nancy;

namespace GeometryServer.Modules
{
    //http://localhost:13775/rest/services/Geometry/GeometryServer/convexhull?sr=4326&geometries={"geometryType":"GeometryPoint","geometries":[{"x":-117.2332208251953,"y":34.086152645433316,"spatialReference":{"wkid":4326}},{"x":-117.21536804199218,"y":34.0854659999255,"spatialReference":{"wkid":4326}},{"x":-117.22498107910155,"y":34.06623992570675,"spatialReference":{"wkid":4326}}]}
    public class ConvexHullModule : NancyModule
    {
        public ConvexHullModule()
            : base("/rest/services/Geometry/GeometryServer/")
        {
            Get["/convexHull"] = parameters =>
            {
                var model = new Models.ConvexHull();

                var exception = new GISServer.Core.Exception();
                exception.Error = new GISServer.Core.Error
                {
                    Code = 400,
                    Message = "Unable to complete convexhull operation."
                };

                #region Parameter


                if (Request.Query["sr"].Value != null)
                {
                    int value;
                    var successful = int.TryParse(Request.Query["sr"].Value, out value);
                    if (successful)
                    {
                        model.SpatialReference = value;
                    }
                    else
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("'sr' parameter is invalid");
                    }
                }

                if (Request.Query["geometries"].Value != null)
                {
                    model.InputGeometries = Request.Query["geometries"].Value;
                }



                if (Request.Query["f"].Value == "json")
                {
                    model.Format = Request.Query["f"].Value;
                }
                else
                {
                    model.Format = "HTML";
                }
                #endregion

                var output_geom_array = new GISServer.Core.Client.Geometry.SingleGeometry();

                if ((model.InputGeometries != null) && (!model.HasException))
                {

                    try
                    {

                        var input_geom_array = new GISServer.Core.Client.Geometry.GeometryArray();
                        input_geom_array = GISServer.Core.Client.Utilities.Parser.GetGeometies(model.InputGeometries);
                        var dotspatial_geom_array = Services.Utilities.getDotSpatialGeometryArray(input_geom_array);
                        var dotspatial_convexhull = Services.Compute.ConvexHull(dotspatial_geom_array);
                        var server_geom_array = Services.Utilities.GetGISSingleGeometry(dotspatial_convexhull);
                        output_geom_array = server_geom_array;
                    }
                    catch (Exception)
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("Unable to generate convexhull for the geometries");
                    }


                }

                 if (!model.HasException)
                {
                    model.Result = output_geom_array.ToJson();
                }
                else
                {
                    model.Exception = exception.ToJson();
                }


                if (model.Format.Equals("json"))
                {
                    if (!model.HasException)
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Result);
                    }
                    else
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Exception);
                    }

                }
                else
                {
                    return View["ConvexHull", model];
                }
            };

        }
    }
}